import type { CodeValueType } from "@/types/piniatypes";
import { defineStore } from "pinia";


// 码表缓存存储类型
interface CodeStoreType {
  systemCodeMap: Record<string, CodeValueType[]>;    // 系统码表缓存
  personalCodeMap: Record<string, CodeValueType[]>;   // 个人码表缓存
}

export const codeStore = defineStore('codeStore', {
  state: (): CodeStoreType => ({
    systemCodeMap: {},
    personalCodeMap: {}
  }),
  persist: {
    storage: localStorage,
    key: 'SYSTEM_CODE_CACHE',
    paths: ['systemCodeMap', 'personalCodeMap']
  },
  getters: {
  },
  actions: {
    // 获取系统码表值
    getSystemCode(bizType: string | string[]): CodeValueType[] | Record<string, CodeValueType[]> {
      if (Array.isArray(bizType)) {
        const resultMap: Record<string, CodeValueType[]> = {};
        bizType.forEach(type => {
          if (this.systemCodeMap[type]) {
            resultMap[type] = this.systemCodeMap[type];
          }
        });
        return resultMap;
      }
      return this.systemCodeMap[bizType] || [];
    },

    // 获取个人码表值
    getPersonalCode(bizType: string | string[]): CodeValueType[] | Record<string, CodeValueType[]> {
      if (Array.isArray(bizType)) {
        const resultMap: Record<string, CodeValueType[]> = {};
        bizType.forEach(type => {
          if (this.personalCodeMap[type]) {
            resultMap[type] = this.personalCodeMap[type];
          }
        });
        return resultMap;
      }
      return this.personalCodeMap[bizType] || [];
    },

    getSystemCodeName(bizType: string, bizValue: string) {
      const codeList = this.systemCodeMap[bizType] || [];
      return codeList.find(item => item.bizValue === bizValue)?.bizName || '';
    },

    // 根据业务类型和值获取名称（个人码表）
    getPersonalCodeName(bizType: string, bizValue: string | number | (string | number)[]): string | string[] {
      const codeList = this.personalCodeMap[bizType] || [];
      if (Array.isArray(bizValue)) {
        return bizValue.map(value =>
          codeList.find(item => item.bizValue === value)?.bizName || ''
        );
      }
      return codeList.find(item => item.bizValue === bizValue)?.bizName || '';
    },

    setInfoBySystemCode(codeList: CodeValueType[]) {
      if (codeList && codeList.length > 0) {
        const codeMap = codeInit(codeList);
        if (codeMap.size > 0) {
          codeMap.forEach((value, key) => {
            this.setSystemCode(key, value);
          });
        }
      }
    },

    // 设置系统码表
    setSystemCode(bizType: string, codeList: CodeValueType[]) {
      this.systemCodeMap[bizType] = codeList;
    },

    // 设置个人码表
    setPersonalCode(bizType: string, codeList: CodeValueType[]) {
      this.personalCodeMap[bizType] = codeList;
    },

    // 清空所有码表缓存
    clearCodeCache() {
      this.systemCodeMap = {};
      this.personalCodeMap = {};
    }
  }
});

//后端授权的模块
const codeInit = (codeList: CodeValueType[]) => {
  const codeMap = new Map<string, CodeValueType[]>();
  for (const item of codeList) {
    if (item.bizType) {
      if (codeMap.has(item.bizType)) {
        codeMap.get(item.bizType)?.push({
          bizValue: item.bizValue,
          bizName: item.bizName
        });
      } else {
        codeMap.set(item.bizType, [{
          bizValue: item.bizValue,
          bizName: item.bizName
        }]);
      }
    }
  }
  return codeMap;
}
